<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .box{
            width: 600px;
            height: 400px;
            border: 3px solid #000;
            margin: 50px auto;
            display: flex;
            flex-direction: column;
        }
        .box>ul{
            height: 60px;
            display: flex;
        }
        .box>ul>li{
            flex: 1;
            color: #000;
            font-size: 30px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;/* 设置光标变为一只手形状 */
        }
        .box>ul>li.active{
            background-color: skyblue;
        }
        .box>ol{
            flex: 1;
        }
        .box>ol>li{
            width: 100%;
            height: 100%;
            background-color: skyblue;
            text-align: center;
            font-size: 30px;
            display: none;
        }

        .box>ol>li.active{
            display: block;
        }
    </style>
</head>
<body>
    <div class="box">
        <ul class="head">
            <li class="active">标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>
        <ol class="main">
            <li class="active">内容一</li>
            <li>内容二</li>
            <li>内容三</li>
        </ol>
    </div>
    <script>
        let btns = document.querySelectorAll("ul > li");
        let boxs = document.querySelectorAll("ol > li");

        for(let i = 0; i < btns.length; i++){
            btns[i].onclick = function(){
                // 将所有的active类名删除（按钮和展示区域）
                for(let j = 0; j < btns.length; j++){
                btns[j].className = "";
                boxs[j].className = ""
            }
            // 为当前点击的那一项添加active类名（按钮和展示区域）
            btns[i].className = "active";
            boxs[i].className = "active";
            }
        }
    //     btns.forEach(function(item,index){
    //     item.onclick = function(){
    //         btns.forEach(function(item2,index2){
    //             item2.classList.remove("active");
    //             boxs[index2].classList.remove("active");
    //         })
    //         item.classList.add("active");
    //         boxs[index].classList.add("active");
    //     }
    // })
    </script>
</body>
</html>